import axios from 'axios'
function maxios(obj){
	let {url,header,data,methods='get',beforeSend,success,timeout='5000'} = obj;
	if(typeof axios=='undefined'){
		console.warn('no install axios,plase install axios!')
	}else{
		let _instance = axios.create({
		header:header,
		timeout:timeout
	})
	let p;
	//拦截请求
	_instance.interceptors.request.use((config)=>{
		beforeSend?beforeSend():''
		return config
	},(err)=>{
		return Promise.reject(err)
	})
	//响应请求
	_instance.interceptors.response.use((config)=>{
		success?success():''
		return config
	},(err)=>{
		return Promise.reject(err)
	})
	
	
	if(methods.toLowerCase()=='get'){
		p = new Promise((resolve,reject)=>{
			_instance.get(url,{
			params:data
		}).then((r)=>{
			resolve(r)
		}).catch((e)=>{
			reject(e)
		})
		})
	}
	if(methods.toLowerCase()=='post'){
		p = new Promise((resolve,reject)=>{
			_instance.post(url,data).then((r)=>{
			resolve(r)
		}).catch((e)=>{
			reject(e)
		})
		})
	}	
	return p
	}
	
	
	
}
export default maxios